<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta charset="utf-8" /></head>

<style type="text/css">
html, body {
padding: 0;
max-width: 900px;
height: 100%;
border: none;
background-color: #F4F8FD;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
div.body{
	border: 1px solid #E0E4EC;
	background-color: #F4F8FD;
}
h1{
	text-align: center;
}
h3{
	text-align: left;
}
div.leftcolum {
float: left;
}
div.rightcolum {
float: right;
}
span.vline {
	border-left: thin solid #CDBFDC;
	border-right: thin solid #CDBFDC;
}
span.hlred {
	font-weight : bold;
	color : #FF0000;
}
table {
	width: 100%;
}
table.textwrap { 
	width: 1px;
	margin: 5px;
}
table.topsection{
	border: 1px solid #CDB;
	background-color: #DEC;
}
td.textleft { text-align: left; }
td.textright { text-align: right; }

SPAN.PermiPrefix { color: #008080 }
SPAN.DataPrefix { color: #22EEAA }
SPAN.CodeStruct { color: #CC00FF; font-weight: bold; }
SPAN.DataType { color: #10CC20 }
SPAN.StmtKey { color: #FF9900; font-weight: bold; }
SPAN.Comment { color: #0066FF; font-style: italic; }
SPAN.String { color: #FF0066 }
SPAN.Number { color: #FF0044 }
SPAN.Method { color: #448800 }
SPAN.SpecSymbol { color: #999900 }
.text_italic { font-style: italic; }
.text_bold { font-weight: bold; }
.text_delete { text-decoration: centerline; }
.text_underline { text-decoration: underline; }
.flushright{ text-align: right; }
.bold{ font-weight: bold; }
.vspace{ padding: 0.3em 0.3em; }
span.codeback{
	background-color: #cda;
}

TABLE.boardHeader {
	padding: 8px 2px;
	background-color: #D5EFDC;
	width: 100%;
}

div.hlcode {
	background-color: #E8F8F8;
	margin: 5px;
	padding : 5px;
	width: 90%;
	margin-left	: 5%;
	margin-right : 5%;
	border: 2px dashed #bdc;
}
TABLE.outputTable {
	background-color: #D8FEFE;
	width: 90%;
	margin-left	: 5%;
	margin-right : 5%;
}
.sdmltable {
	background-color: #F8E8FB;
	width: 90%;
	margin-left	: 5%;
	margin-right : 5%;
	border: 1px solid #CCCCCC;
}
.docTableRow {
	padding: 10px 10px;
	background-color: #F4EEFB;
}
.docTableCell {
	background-color: #F8EEFB;
	padding-right  : 10px;
	padding-top	: 2px;
	padding-left   : 10px;
	padding-bottom : 2px;
	border: 1px solid #CCCCCC;
}

</style>
<body><div class="body">
<h2>道语言异常处理</h2>
 <br/>
道异常类被组织为树形结构，其基类是 Exception：
<div class="hlcode">
<span class=CodeStruct>class</span>&thinsp; Exception<br/><span class=SpecSymbol>{</span><br/>&thinsp; &thinsp; <span class=CodeStruct>routine</span>&thinsp; Exception<span class=SpecSymbol>(</span>&thinsp; content=<span class=String>''</span>&thinsp; <span class=SpecSymbol>)</span><span class=SpecSymbol>{</span>&thinsp; Content&thinsp; =&thinsp; content&thinsp; <span class=SpecSymbol>}</span><br/>&thinsp; &thinsp; <span class=PermiPrefix>protected</span><br/>&thinsp; &thinsp; &thinsp; &thinsp; var&thinsp; Rout&thinsp; =&thinsp; <span class=String>''</span>;<br/>&thinsp; &thinsp; &thinsp; &thinsp; var&thinsp; File&thinsp; =&thinsp; <span class=String>''</span>;<br/>&thinsp; &thinsp; &thinsp; &thinsp; var&thinsp; FromLine&thinsp; =&thinsp; <span class=Number>0</span>;<br/>&thinsp; &thinsp; &thinsp; &thinsp; var&thinsp; ToLine&thinsp; =&thinsp; <span class=Number>0</span>;<br/>&thinsp; &thinsp; <span class=PermiPrefix>public</span><br/>&thinsp; &thinsp; &thinsp; &thinsp; var&thinsp; Name&thinsp; &thinsp; =&thinsp; <span class=String>'Exception'</span>;<br/>&thinsp; &thinsp; &thinsp; &thinsp; var&thinsp; Content&thinsp; :&thinsp; any&thinsp; =&thinsp; <span class=String>'undefined exception'</span>;<br/><span class=SpecSymbol>}</span></div>
 
目前支持的类包括:

<table class="sdmltable">
<tr><th> 类</th><th> 异常类型</th></tr>
<tr class=docTableRow><td class=docTableCell> Exception</td><td class=docTableCell> Any</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.None</td><td class=docTableCell> None</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Any</td><td class=docTableCell> Any or none</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error</td><td class=docTableCell> Any error</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Warning</td><td class=docTableCell> Any warning</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Field</td><td class=docTableCell> invalid field accessing</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Field.NotExist</td><td class=docTableCell> field not exist</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Field.NotPermit</td><td class=docTableCell> field not permit</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Float</td><td class=docTableCell> invalid floating point operation</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Float.DivByZero</td><td class=docTableCell> division by zero</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Float.OverFlow</td><td class=docTableCell> floating point overflow</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Float.UnderFlow</td><td class=docTableCell> floating point underflow</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Index</td><td class=docTableCell> invalid index</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Index.OutOfRange</td><td class=docTableCell> index out of range</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Key</td><td class=docTableCell> invalid key</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Key.NotExist</td><td class=docTableCell> key not exist</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Param</td><td class=docTableCell> invalid parameter list for the call</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Syntax</td><td class=docTableCell> invalid syntax</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Type</td><td class=docTableCell> invalid variable type for the operation</td></tr>
<tr class=docTableRow><td class=docTableCell> Exception.Error.Value</td><td class=docTableCell> invalid variable value for the operation</td></tr>
</table>
 
从以上类可以派生出新的异常类。<br/><br/>
任何运行时错误将抛出异常类实例。
程序也可在任何地方使用raise语句抛出异常类实例。
被抛出的异常可由rescue语句扑获，然后进行处理，
相关语法是：
<div class="hlcode">
stmt_tryrescue&thinsp; <span class=CodeStruct>::=</span>&thinsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp; <span class=String>'try'</span>&thinsp; <span class=String>'{'</span>&thinsp; stmt_block<br /> &nbsp;&nbsp;&nbsp;&nbsp; <span class=SpecSymbol>{</span>&thinsp; <span class=String>'}'</span>&thinsp; <span class=String>'rescue'</span>&thinsp; <span class=SpecSymbol>[</span>&thinsp; <span class=String>'('</span>&thinsp; expr&thinsp; <span class=String>')'</span>&thinsp; <span class=SpecSymbol>]</span>&thinsp; <span class=String>'{'</span>&thinsp; stmt_block&thinsp; <span class=SpecSymbol>}</span><span class=DataType>*</span>&thinsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp; <span class=String>'}'</span></div>
 
这里expr可以为空，或是一个或多个异常类。
如果rescue后没有expr，所有异常都将被扑获。
每个try-rescue块里，可以有多个rescue语句，
每个rescue语句都将被检测是否可以扑获到异常，
如果扑获到了，相应的语句块将被执行，
并且被扑获的异常将可用全局变量exceptions列表访问。<br/><br/>
如果所有rescue语句都被检测了，但还剩没有扑获的异常，
那么当前运行函数将中止，并将异常抛给起调用者。
如果当前函数没有调用者，那么程序将被中止，
并打印异常信息。
在一个rescue语句块里，在修改某些运行条件后，retry可用来重新尝试
执行try语句块里的代码。<br/><br/>
例子，
<div class="hlcode">
try<span class=SpecSymbol>{</span><br/>&nbsp;&nbsp;&nbsp;&nbsp; raise&thinsp; Exception.Error<span class=SpecSymbol>(</span>&thinsp; <span class=String>"error test"</span>&thinsp; <span class=SpecSymbol>)</span>,&thinsp; Exception.Warning<span class=SpecSymbol>(</span>&thinsp; <span class=String>"warning test"</span>&thinsp; <span class=SpecSymbol>)</span>,<br/>&nbsp;&nbsp;&nbsp;&nbsp; Exception<span class=SpecSymbol>(</span>&thinsp; <span class=String>"exception"</span>&thinsp; <span class=SpecSymbol>)</span>;<br/><span class=SpecSymbol>}</span>rescue<span class=SpecSymbol>(</span>&thinsp; Exception.Error,&thinsp; Exception.Warning&thinsp; <span class=SpecSymbol>)</span><span class=SpecSymbol>{</span><br/>&nbsp;&nbsp;&nbsp;&nbsp; <span class=Method>stdio.println</span><span class=SpecSymbol>(</span>&thinsp; exceptions&thinsp; <span class=SpecSymbol>)</span>;<br/><span class=SpecSymbol>}</span>rescue<span class=SpecSymbol>{</span><br/>&nbsp;&nbsp;&nbsp;&nbsp; <span class=Method>stdio.println</span><span class=SpecSymbol>(</span>&thinsp; <span class=String>"rescued"</span>&thinsp; <span class=SpecSymbol>)</span>;<br/><span class=SpecSymbol>}</span></div>
<br clear=all />
</div></body></html>
